/*
 * Copyright 2009 Rohan Ranade
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.fcr.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingWorker;

/**
 *
 * @author Rohan Ranade
 */
public class GenericProgressDialog extends javax.swing.JDialog {

    private final SwingWorker<?, ?> worker;
    private final boolean indeterminate;

    /** Creates new form ProgressDialog */
    public GenericProgressDialog(SwingWorker<?, ?> worker,
            java.awt.Frame parent, boolean modal) {
        this(worker, parent, modal, false);
    }

    public GenericProgressDialog(SwingWorker<?, ?> worker,
            java.awt.Frame parent, boolean modal, boolean indeterminate) {
        super(parent, modal);
        this.worker = worker;
        this.indeterminate = indeterminate;
        initComponents();
        this.progress.setIndeterminate(indeterminate);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progress = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Progress");

        progress.setStringPainted(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar progress;
    // End of variables declaration//GEN-END:variables

    @Override
    public void addNotify() {
        super.addNotify();
        worker.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (!indeterminate && "progress".equals(evt.getPropertyName())) {
                    int progressVal = (Integer) evt.getNewValue();
                    progress.setValue(progressVal);
                } else if ("state".equals(evt.getPropertyName())) {
                    if (SwingWorker.StateValue.DONE == (SwingWorker.StateValue) evt.getNewValue()) {
                        setVisible(false);
                    }
                }
            }
        });
        worker.execute();
    }
}
